package hanglers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"penguin-hub/cache"
	"penguin-hub/models"
	"penguin-hub/pkg"
	"strconv"
	"time"
)

func User(c *gin.Context) {
	name := c.PostForm("name")
	password := c.PostForm("password")
	if name == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	var user models.User
	redis_key := "user" + name
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误三次禁止登录",
		})
		return
	}
	err := models.DB.Where("name = ?", name).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "未找到该用户",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名错误",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次以上 封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
		"data": user,
	})
	return
}

func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Role(c *gin.Context) {
	uid := c.Query("uid")

	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.id =roles.id
	*/
	var role []models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid =roles.id").
		Where("user_roles.uid = ?", uid).
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return

}
func Node(c *gin.Context) {
	rid := c.Query("rid")
	/*
			SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.id =nodes.id
	*/
	var node models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.id =nodes.id").
		Where("role_nodes.id = ?", rid).
		Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "1"))
	offset := (page - 1) * size
	/*
		SELECT
		  car_orders.id as Oid,
		  cars.car_name as CarName,
		  cars.car_price as CarPrice,
		  cars.car_tel as CarTel,
		  car_orders.car_num as CarNum,
		  car_orders.car_km as CarKm
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
	*/
	var list []models.CarOrderList
	redis_key := "list" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id as Oid",
				"cars.car_name as CarName",
				"cars.car_price as CarPrice",
				"cars.car_tel as CarTel",
				"car_orders.car_num as CarNum",
				" car_orders.car_km as CarKm",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	var total int64
	models.DB.Model(models.CarOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"total": total,
			"list":  list,
		},
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  car_orders.id as Oid,
		  cars.car_name as CarName,
		  cars.car_price as CarPrice,
		  cars.car_tel as CarTel,
		  car_orders.car_num as CarNum,
		  car_orders.car_km as CarKm
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
	*/
	var list []models.CarOrderList
	redis_key := "detail" + id
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id as Oid",
				"cars.car_name as CarName",
				"cars.car_price as CarPrice",
				"cars.car_tel as CarTel",
				"car_orders.car_num as CarNum",
				" car_orders.car_km as CarKm",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Where("car_orders.id = ?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
